from GuiCore import *
from GUI import Ui_RightColumn
from GUI import Ui_MainPages
from GuiCore.widgets import *
from GuiCore.widgets.py_credits_bar.py_credits import PyCredits
from qt_core import *


class UI_MainWindow(object):

    def setup_ui(self, parent):
        if not parent.objectName():
            parent.setObjectName("MainWindow")
        # 加载资源
        self.settings = parent.settings
        self.update_log = parent.update_log
        self.app_lang = parent.app_lang
        self.menu_left = parent.menu_left
        self.menu_title = parent.menu_title
        self.ColorPalette = parent.ColorPalette
        # 设置初始化窗口大小
        parent.resize(
            self.settings["startup_size"][0], self.settings["startup_size"][1]
        )
        parent.setMinimumSize(
            self.settings["minimum_size"][0], self.settings["minimum_size"][1]
        )
        # 设置主体
        self.central_widget = QWidget()
        self.central_widget.setStyleSheet(
            f"""
            font: {self.settings["font"]["text_size"]}pt "{self.settings["font"]["family"]}";
            color: {self.ColorPalette.text_foreground};
        """
        )
        self.central_widget_layout = QVBoxLayout(self.central_widget)
        if self.settings["custom_title_bar"]:
            self.central_widget_layout.setContentsMargins(10, 10, 10, 10)
        else:
            self.central_widget_layout.setContentsMargins(0, 0, 0, 0)
        # 加载PyWindow自定义小部件
        # 在PyWindow布局中添加所有小部件
        self.window = PyWindow(
            parent,
            ColorPalette=self.ColorPalette,
            text_font=f'{self.settings["font"]["text_size"]}"{self.settings["font"]["family"]}"',
        )
        # 如果禁用自定义标题栏
        if not self.settings["custom_title_bar"]:
            self.window.set_stylesheet(border_radius=0, border_size=0)
        # 将PyWindow添加到中心小部件
        self.central_widget_layout.addWidget(self.window)
        # 添加框架左侧菜单
        # 在此处添加自定义左侧菜单栏
        left_menu_margin = self.settings["left_menu_content_margins"]
        left_menu_minimum = self.settings["lef_menu_size"]["minimum"]
        self.left_menu_frame = QFrame()
        self.left_menu_frame.setMaximumSize(
            left_menu_minimum + (left_menu_margin * 2), 17280
        )
        self.left_menu_frame.setMinimumSize(
            left_menu_minimum + (left_menu_margin * 2), 0
        )
        # 左侧菜单布局
        self.left_menu_layout = QHBoxLayout(self.left_menu_frame)
        self.left_menu_layout.setContentsMargins(
            left_menu_margin, left_menu_margin, left_menu_margin, left_menu_margin
        )
        # 添加左侧菜单
        # 在此处添加自定义左菜单
        self.left_menu = PyLeftMenu(
            parent=self.left_menu_frame,
            app_parent=self.central_widget,  # For tooltip parent
            ColorPalette=self.ColorPalette,
            minimum_width=self.settings["lef_menu_size"]["minimum"],
            maximum_width=self.settings["lef_menu_size"]["maximum"],
            toggle_text=self.app_lang["UI"]["ui_Hide"],
            toggle_tooltip=self.app_lang["UI"]["ui_Show"],
            duration_time=self.settings["time_animation"],
        )
        self.left_menu_layout.addWidget(self.left_menu)
        # 添加左列
        # 在此处添加带有Stacked Widgets的左列
        self.left_column_frame = QFrame()
        self.left_column_frame.setMaximumWidth(
            self.settings["left_column_size"]["minimum"]
        )
        self.left_column_frame.setMinimumWidth(
            self.settings["left_column_size"]["minimum"]
        )
        self.left_column_frame.setStyleSheet(f"background: {self.ColorPalette.bg_two}")
        # 向左列添加布局
        self.left_column_layout = QVBoxLayout(self.left_column_frame)
        self.left_column_layout.setContentsMargins(0, 0, 0, 0)
        # 添加自定义左菜单小部件
        self.left_column = PyLeftColumn(
            parent,
            app_parent=self.central_widget,
            ColorPalette=self.ColorPalette,
            text_title="我的世界",
            text_title_size=self.settings["font"]["title_size"],
            icon_path=Functions.set_svg_icon("icon_more.svg"),
            icon_close_path=Functions.set_svg_icon("icon_close.svg"),
            font_family=self.settings["font"]["family"],
        )
        self.left_column_layout.addWidget(self.left_column)
        # 添加右侧小部件
        self.right_app_frame = QFrame()
        # 添加右侧应用程序布局
        self.right_app_layout = QVBoxLayout(self.right_app_frame)
        self.right_app_layout.setContentsMargins(3, 3, 3, 3)
        self.right_app_layout.setSpacing(6)
        # 添加标题栏框架、布局
        self.title_bar_frame = QFrame()
        self.title_bar_frame.setMinimumHeight(40)
        self.title_bar_frame.setMaximumHeight(40)
        self.title_bar_layout = QVBoxLayout(self.title_bar_frame)
        self.title_bar_layout.setContentsMargins(0, 0, 0, 0)
        # 在布局中添加自定义标题栏
        self.title_bar = PyTitleBar(
            parent,
            ColorPalette=self.ColorPalette,
            logo_width=100,
            app_parent=self.central_widget,
            logo_image=self.settings["logo_title"],
            radius=8,
            font_family=self.settings["font"]["family"],
            title_size=self.settings["font"]["title_size"],
            is_custom_title_bar=self.settings["custom_title_bar"],
            custom_title_minimize_button_text=self.app_lang["UI"]["ui_Minimize"],
            custom_title_maximize_button_text=self.app_lang["UI"]["ui_Maximize"],
            custom_title_close_text=self.app_lang["UI"]["ui_Close"],
        )
        self.title_bar_layout.addWidget(self.title_bar)
        # 添加内容框架、布局
        self.content_area_frame = QFrame()
        self.content_area_layout = QHBoxLayout(self.content_area_frame)
        self.content_area_layout.setContentsMargins(0, 0, 0, 0)
        self.content_area_layout.setSpacing(0)
        # 左侧内容
        self.content_area_left_frame = QFrame()
        # 将主页导入到内容区域
        # TODO
        self.load_pages = Ui_MainPages()
        self.load_pages.setupUi(self.content_area_left_frame)
        # 右侧工具栏
        self.right_column_frame = QFrame()
        self.right_column_frame.setMinimumWidth(
            self.settings["right_column_size"]["minimum"]
        )
        self.right_column_frame.setMaximumWidth(
            self.settings["right_column_size"]["minimum"]
        )
        # 导入右列
        self.content_area_right_layout = QVBoxLayout(self.right_column_frame)
        self.content_area_right_layout.setContentsMargins(5, 5, 5, 5)
        self.content_area_right_layout.setSpacing(0)
        # 右背景
        self.content_area_right_bg_frame = QFrame()
        self.content_area_right_bg_frame.setObjectName("content_area_right_bg_frame")
        self.content_area_right_bg_frame.setStyleSheet(
            f"""
        #content_area_right_bg_frame {{
            border-radius: 8px;
            background-color: {self.ColorPalette.bg_two};
        }}
        """
        )
        # ADD BG
        self.content_area_right_layout.addWidget(self.content_area_right_bg_frame)
        # ADD RIGHT PAGES TO RIGHT COLUMN
        self.right_column = Ui_RightColumn()
        self.right_column.setupUi(self.content_area_right_bg_frame)
        # ADD TO LAYOUTS
        self.content_area_layout.addWidget(self.content_area_left_frame)
        self.content_area_layout.addWidget(self.right_column_frame)
        # CREDITS / BOTTOM APP FRAME
        # ///////////////////////////////////////////////////////////////
        self.credits_frame = QFrame()
        self.credits_frame.setMinimumHeight(26)
        self.credits_frame.setMaximumHeight(26)
        # CREATE LAYOUT
        self.credits_layout = QVBoxLayout(self.credits_frame)
        self.credits_layout.setContentsMargins(0, 0, 0, 0)
        # ADD CUSTOM WIDGET CREDITS
        self.credits = PyCredits(
            bg_two=self.ColorPalette.bg_two,
            copyright=self.app_lang["SYS"]["sys_copyright"],
            version=self.app_lang["SYS"]["sys_version"],
            font_family=self.settings["font"]["family"],
            text_size=self.settings["font"]["text_size"],
            text_description_color=self.ColorPalette.text_description,
        )
        #  ADD TO LAYOUT
        self.credits_layout.addWidget(self.credits)
        # 将小部件添加到右侧布局
        self.right_app_layout.addWidget(self.title_bar_frame)
        self.right_app_layout.addWidget(self.content_area_frame)
        self.right_app_layout.addWidget(self.credits_frame)
        # 将控件添加到“PyWindow”
        self.window.layout.addWidget(self.left_menu_frame)
        self.window.layout.addWidget(self.left_column_frame)
        self.window.layout.addWidget(self.right_app_frame)
        # 添加中心小部件并设置内容边距
        parent.setCentralWidget(self.central_widget)
